/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wxk.Bonus.Main;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.SplashScreen;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import wxk.Bonus.Constant.GlobalConfig;
import wxk.Bonus.utils.BonusLogger;
import wxk.Bonus.utils.ScreenUtil;

/**
 *
 * @author William Wang
 */
public class BonusGate extends Application {
     static SplashScreen splash=null; 
     static GlobalConfig globalconfig=GlobalConfig.getInstance();
    @Override
    public void start(Stage primaryStage) {
        
       globalconfig.setGlobalStage(primaryStage);
        try
        {
       Pane root = (Pane)FXMLLoader.load(BonusGate.class.getResource("/wxk/Bonus/FXML/Sample.fxml"));
//fill screen side
ScreenUtil sutil=ScreenUtil.getInstance();
int taskbarheight=sutil.getTaskBarHeight();

Scene scene = new Scene(root, sutil.getAvaliable_screen_width(), 
        sutil.getAvaliable_screen_height()-taskbarheight);
        primaryStage.setResizable(false);
renderSplashFrame("SytleSheet Resource...");         
    //add style sheet.
    scene.getStylesheets().add
 (BonusGate.class.getResource("/wxk/Bonus/Css/StyleSheet.css").toExternalForm());    
        primaryStage.setTitle("Bonus Wizard 2015");
        primaryStage.setScene(scene);
        closeSplash();
        primaryStage.show();
 //get windows manager give titlebar height;
       sutil.setTitlebar_height(primaryStage.getHeight()-scene.getHeight());
       
        }catch(Exception e)
        {
            e.printStackTrace();
        }
   globalconfig.setGlobalStage(primaryStage);
   globalconfig.createTranslate();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                Platform.exit();
            }
       });        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        splash();
        FirstRun frun = new FirstRun();
        if(frun.isFirst())
        {
         renderSplashFrame("First Run,Do System initialize....");   
          frun.runFirstBusiness();
        }
        launch(args);
    }
private static void splash()
{
 splash = SplashScreen.getSplashScreen();
 if(splash==null)
 {
      BonusLogger.getInstance().log("Splash screen is null");
      return;   
 }
 
}
public static void renderSplashFrame( String msg) 
 {
   System.out.println(Thread.currentThread().toString());  
    if(splash==null)return; 
        Graphics2D g = splash.createGraphics();
        if (g == null) {
            BonusLogger.getInstance().log("g is null");
            return;
        }
        g.setColor(Color.RED);
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,40);
        g.setPaintMode();
        g.setColor(Color.BLUE);
        g.drawString("Loading "+msg+"...", 60, 141);
       splash.update(); 
    }
 void closeSplash()
 {
     if(splash!=null)splash.close();     
 }
}
